package com.phuc.game.tankshooter.engine;

import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.util.MathUtils;

import android.util.Log;

import com.phuc.game.tankshooter.MainActivity;

public class MovingPathFactory {

	public static final int SIZE_OBJECT_WIDTH = 80;
	public static final int SIZE_OBJECT_HEIGHT = 100;
	
	public static final int PATH_TYPE_STRAIGHT_TOP_BOTTOM = 1001;
	public static final int PATH_TYPE_LINE_TOP_BOTTOM = 1002;
	public static final int PATH_TYPE_ZIGZAG_RANDOM_TOP_BOTTOM = 1003;
	public static final int PATH_TYPE_ZIGZAG_2_TOP_BOTTOM = 1008;
	public static final int PATH_TYPE_ZIGZAG_3_TOP_BOTTOM = 1009;
	public static final int PATH_TYPE_ZIGZAG_4_TOP_BOTTOM = 1010;
	public static final int PATH_TYPE_ZIGZAG_5_TOP_BOTTOM = 1011;
	public static final int PATH_TYPE_ZIGZAG_STANDARD_2_TOP_BOTTOM = 1012;
	public static final int PATH_TYPE_ZIGZAG_STANDARD_3_TOP_BOTTOM = 1013;
	public static final int PATH_TYPE_ZIGZAG_STANDARD_4_TOP_BOTTOM = 1014;

	public static final int PATH_TYPE_FLY_LEFT_RIGHT = 1004;
	public static final int PATH_TYPE_FLY_RIGHT_LEFT = 1005;
	public static final int PATH_TYPE_FLY_ZIGZAG_RIGHT_LEFT = 1006;
	public static final int PATH_TYPE_FLY_ZIGZAG_LEFT_RIGHT = 1007;
	
	public static Path[] createRandomPath(int ...pathType) {
		int randomNum = MathUtils.random(0, pathType.length - 1);
		int pathTypeSelected = pathType[randomNum];
		int x1;
		int y1;
		int randomNumberTmp;
		Path [] result;
		int numberOfPath = 1;
		
		switch (pathTypeSelected) {
		/**
		 * Tao 1 duong di thang vuong goc tu tren xuong duoi
		 */
		case PATH_TYPE_STRAIGHT_TOP_BOTTOM:
			x1 = MathUtils.random(0,
					MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			y1 = -SIZE_OBJECT_HEIGHT;
			result = new Path[] {createRandomStraightPath_TopToBottom(x1, y1)};
			return result;
		/**
		 * Tao 1 duong thang cheo bat ky tu tren xuong duoi
		 */
		case PATH_TYPE_LINE_TOP_BOTTOM:
			x1 = MathUtils.random(0,
					MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			y1 = -SIZE_OBJECT_HEIGHT;
			result = new Path[] {createRandomLinePath_TopToBottom(x1, y1)};
			return result;
		/**
		 * Tao duong di zigzac tu tren xuong duoi voi so duong gap khuc
		 */
		case PATH_TYPE_ZIGZAG_5_TOP_BOTTOM: numberOfPath++;
		case PATH_TYPE_ZIGZAG_4_TOP_BOTTOM: numberOfPath++;
		case PATH_TYPE_ZIGZAG_3_TOP_BOTTOM: numberOfPath++;
		case PATH_TYPE_ZIGZAG_2_TOP_BOTTOM: numberOfPath++;
		/**
		 * Tao duong di zigzac tu tren xuong duoi,
		 * voi so duong gap khuc bat ky (2 -> 3)
		 */
		case PATH_TYPE_ZIGZAG_RANDOM_TOP_BOTTOM: {
			if (numberOfPath > 1) {
				randomNumberTmp = numberOfPath;
			} else {
				randomNumberTmp = MathUtils.random(2, 3);
			}
			result = new Path[randomNumberTmp];
			int fromX = MathUtils.random(0, MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			int fromY = -SIZE_OBJECT_HEIGHT;
			int toX = MathUtils.random(0, MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			int toY = MainActivity.SCENE_END_LOCATION / randomNumberTmp;
			result[0] = new Path(2).to(fromX, fromY).to(toX, toY);
			
			for (int i = 1; i < randomNumberTmp; i++) {
				fromX = toX;
				fromY = toY;
				toY = MainActivity.SCENE_END_LOCATION * (i + 1) / randomNumberTmp;
				toX = MathUtils.random(0, MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
				result[i] = new Path(2).to(fromX, fromY).to(toX, toY);
			}
			Log.d("phucphuc", "phucphuc hihi " + randomNumberTmp);
			return result;
		}
		case PATH_TYPE_ZIGZAG_STANDARD_4_TOP_BOTTOM:numberOfPath++;
		case PATH_TYPE_ZIGZAG_STANDARD_3_TOP_BOTTOM:numberOfPath++;
		case PATH_TYPE_ZIGZAG_STANDARD_2_TOP_BOTTOM: {
			numberOfPath++;
			result = new Path[numberOfPath];
			int side = MathUtils.random(0, 1);
			
			int fromX = (MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH) * (side % 2);
			int fromY = -SIZE_OBJECT_HEIGHT;
			side++;
			int toX = (MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH) * (side % 2);
			int toY = MainActivity.SCENE_END_LOCATION / numberOfPath;
			result[0] = new Path(2).to(fromX, fromY).to(toX, toY);
			
			for (int i = 1; i < numberOfPath; i++) {
				fromX = toX;
				fromY = toY;
				side++;
				toX = (MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH) * (side % 2);
				toY = MainActivity.SCENE_END_LOCATION * (i + 1) / numberOfPath;
				result[i] = new Path(2).to(fromX, fromY).to(toX, toY);
			}
			
			return result;
		}
		/**
		 * Tao 1 duong bay thang ngang man hinh, trai qua phai
		 */
		case PATH_TYPE_FLY_LEFT_RIGHT:

			break;
		/**
		 * Tao 1 duong bay thang ngang man hinh, phai qua trai
		 */
		case PATH_TYPE_FLY_RIGHT_LEFT:

			break;
		case PATH_TYPE_FLY_ZIGZAG_RIGHT_LEFT:

			break;
		case PATH_TYPE_FLY_ZIGZAG_LEFT_RIGHT:

			break;

		default:
			break;
		}

		return null;
	}
	
	public static Path createRandomPath() {
		int random = MathUtils.random(0, 1);
		int x1;
		int y1;
		switch (random) {
		case 0:
			x1 = MathUtils.random(0,
					MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			y1 = -SIZE_OBJECT_HEIGHT;
			return createRandomStraightPath_TopToBottom(x1, y1);
		case 1:
			x1 = MathUtils.random(0,
					MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			y1 = -SIZE_OBJECT_HEIGHT;
			return createRandomLinePath_TopToBottom(x1, y1);
		default:
			x1 = MathUtils.random(0,
					MainActivity.SCENE_WIDTH - SIZE_OBJECT_WIDTH);
			y1 = -SIZE_OBJECT_HEIGHT;
			return createRandomStraightPath_TopToBottom(x1, y1);
		}
	}
	
	public static Path createRandomStraightPath_TopToBottom(int x1, int y1) {
		final int x2 = x1;
		final int y2 = MainActivity.SCENE_END_LOCATION;
		Path path = new Path(2).to(x1, y1).to(x2, y2);
		return path;
	}

	public static Path createRandomLinePath_TopToBottom(int x1, int y1) {
		final int x2 = MathUtils.random(0, MainActivity.SCENE_WIDTH - 100);
		final int y2 = MainActivity.SCENE_END_LOCATION;
		Path path = new Path(2).to(x1, y1).to(x2, y2);
		return path;
	}

	public static Path createZigzagPath_1(int x1, int y1) {
		final int x2 = 0;
		final int y2 = 0;
		Path path = new Path(2).to(x1, y1).to(x2, y2);
		return path;
	}
	
	public static Path createLinePath(int x1, int y1, int x2, int y2) {
		Path path = new Path(2).to(x1, y1).to(x2, y2);
		return path;
	}
	
//	public static Path createZigZagPath() {
//		
//	}
}
